<template>
    <div>
        <!-- 顶部导航栏 开始 -->
        <div class="header">
            <van-nav-bar title="分享活动" left-arrow @click-left="back" />
        </div>
        <div class="imgBox" ref="imageTofile" data-screenshot="true">
            <template v-if="imgList.length != 0">
                <van-swipe ref="swiperR" class="my-swipe" autoplay="false" indicator-color="white"
                    @change="swiperChange">
                    <van-swipe-item v-for="(item, index) in imgList" :key="index">
                        <div class="imgCon">
                            <img :src="imgPath + 'zhonghui/share/' + item.imgName" alt="">
                        </div>
                    </van-swipe-item>
                </van-swipe>
                <div class="swiperBtn" v-if="!showPrice">
                    <div @click="swiperHandle('1')">
                        <template v-if="imgList.length > 1">
                            <van-icon name="arrow-left"></van-icon>
                            上一张
                        </template>
                    </div>
                    <img src="../../assets/distribution/new/createBtn.png" alt="" @click="dowbHandle(swiperIndex)"
                        class="saveDownBtn">
                    <div @click="swiperHandle('2')">
                        <template v-if="imgList.length > 1">
                            下一张
                            <van-icon name="arrow"></van-icon>
                        </template>
                    </div>
                </div>
            </template>
            <img src="../../assets/distribution/empty.png" alt="" class="emptyBoxImg" v-else>

            <img src="../../assets/distribution/new/downImg.png" class="downImg" alt="" v-if="!showPrice">
            <template v-if="imgList.length != 0">
                <div class="handleBtn" v-if="!showPrice">
                    <img src="../../assets/distribution/new/jiangBtn.png" alt="" @click="showPrice = true">
                    <img src="../../assets/distribution/new/wxBtn.png" alt="" @click="showKefu = true">
                </div>
            </template>
        </div>
        <div class="ruleBox" v-if="!showPrice">
            <img src="../../assets/distribution/new/ruleTit.png" class="ruleTitle" alt="">
            <img src="../../assets/distribution/new/ruleTop.png" class="topImg" alt="">
            <img src="../../assets/distribution/new/rulesLeft.png" class="leftImg" alt="">
            <img src="../../assets/distribution//new/ruleBottom.png" class="bottomImg" alt="">
            <div class="rulesCon" v-html="shareRules" v-if="shareRules != ''"></div>
            <img src="../../assets/distribution/rules.png" class="ruleImg" alt="" v-else>
        </div>
        <!-- 抽奖图标 -->
        <img src="../../assets/distribution/jiangImg.png" class="jiangImg" @click="tojiang" alt="" v-if="!showPrice">


        <!-- 上传图片弹窗 -->
        <van-overlay :show="showPrice" :duration="0">
            <div class="showdiaBox">
                <div class="dialog">
                    <img class="price-top" src="../../assets/distribution/price-top.png" alt="">
                    <div class="dia-title">温馨提示</div>
                    <div class="dia-text">请仔细下方阅读活动规则</div>
                    <div class="dia-text">上传符合活动要求的朋友圈截图</div>
                    <div class="dia-text">等待工作人员审核，将为您发放抽奖资格
                    </div>
                    <div class="upimgBox">
                        <van-uploader v-model="shareImg" @delete="delHandle" :max-count="1" :after-read="ziZhiHandle">
                            <div :class="shareImg.length != 0 ? 'uplader' : 'firstUplader'"><van-icon
                                    name="plus"></van-icon>朋友圈截图
                            </div>
                        </van-uploader>
                    </div>
                    <div class="btnBox">
                        <div class="dia-btn" @click="toGet">立即提交</div>
                        <div class="waitBtn" @click="closeHandle">暂不提交</div>
                    </div>
                </div>
            </div>
        </van-overlay>

        <!-- 图片预览弹窗 -->
        <van-overlay :show="showImgBox" :duration="0">
            <div class="imgOverBox">
                <div class="conBox">
                    <img :src="downImgUrl" alt="">
                </div>
                <div class="imgBtnBox">
                    <div @click="canleHandle">取消</div>
                    <div @click="saveImg(downImgUrl)">保存</div>
                </div>
            </div>
        </van-overlay>


        <!-- 客服微信弹窗 -->
        <van-popup v-model="showKefu" round closeable>
            <div class="kefuImgBox">
                <img :src="kefuImg" alt="">
                <div @click="saveImg(kefuImg)">保存客服微信</div>
            </div>
        </van-popup>
    </div>

</template>

<script>

import { addActivitySharingRecord, listUserImg, getActivitySharing, getRuleList, listShare } from '../../network/active'
import { alPath } from "../../network/request";
export default {
    name: 'HomeR',
    data() {
        return {
            imgPath: alPath,
            showPrice: false,
            isShow: true,
            imgList: [],
            shareRules: '',
            shareImg: [],
            images: '',
            showImgBox: false,
            swiperIndex: 0,
            downImgUrl: '',
            showKefu: false,
            kefuImg: '',


        }
    },
    mounted() {

    },
    methods: {

        canleHandle() {
            this.downImgUrl = ''
            this.showImgBox = false
        },

        saveImg(url) {

            if (plus.os.name == 'iOS') {
                plus.runtime.openURL(url, function (res) {
                    console.log(res);
                });
            } else {
                // window.location.href = url
                var link = document.createElement('a');
                link.style.display = 'none';
                document.body.appendChild(link);
                link.href = url;
                link.download = '海报';
                link.click();
                document.body.removeChild(link);
                console.log(url);
            }
        },


        swiperHandle(flag) {
            if (flag == 1) {
                this.$refs.swiperR.prev()
            } else if (flag == 2) {
                this.$refs.swiperR.next()
            }

        },
        swiperChange(index) {
            this.swiperIndex = index

            console.log(this.swiperIndex);
        },



        toGet() {
            if (this.shareImg.length == 0) {
                this.$toast('请上传朋友圈截图')
                return
            }
            console.log(this.shareImg);

            var formData = new FormData();

            this.shareImg.forEach(ele => {
                formData.append("file", ele.file);
            })
            formData.append("userId", localStorage.userId);
            formData.append("shopId", localStorage.shopId);
            formData.append("recordFlag", 0);
            formData.append("recordChou", 0);
            formData.append("flag", 'add');


            addActivitySharingRecord(formData).then(res => {
                if (res.flag) {
                    this.shareImg = []
                    this.showPrice = false
                    this.$toast('提交成功，请等待审核')
                } else {
                    this.$toast('提交失败')
                }
            })
        },

        closeHandle() {
            this.showPrice = false
            this.shareImg = []
        },

        dowbHandle(index) {

            this.getMyPic(this.imgList[index].imgName)


        },

        getMyPic(picName) {
            listUserImg({
                shopId: localStorage.shopId,
                userId: localStorage.userId,
                picName
            }).then(res => {
                if (res.flag) {
                    if (res.data.flag == 'error') {
                        this.$toast('生成失败')
                    } else {
                        this.showImgBox = true
                        this.downImgUrl = res.data.url
                        console.log(res.data.url);
                    }
                }
            })


        },
        getData: function () {
            listShare({
                page: 1,
                size: 100,
                imgFlag: 0
            }).then(res => {
                if (res.flag) {
                    this.imgList = res.data.list
                }
            })
            listShare({
                page: 1,
                size: 100,
                imgFlag: 1
            }).then(res => {
                if (res.flag) {
                    this.kefuImg = this.imgPath + 'zhonghui/weixin/' + res.data.list[0].imgName
                }
            })
        },


        getRuleList() {
            getRuleList().then(res => {
                if (res.flag) {
                    res.data.forEach(ele => {
                        if (ele.ruleFlag == 2) {
                            this.shareRules = ele.ruleContent
                        }
                    })
                }
            })
        },

        back: function () {
            mui.back()
        },
        tojiang() {
            // 抽奖

            this.$router.push('/draw')

        },
        delHandle() {
            console.log(this.shareImg);
        },
        ziZhiHandle() {
            console.log(this.shareImg);
        },

        back() {
            this.$router.go(-1)
        },





    },
    created() {
        this.getData()

        this.getRuleList()
    },
    //计算属性
    computed: {

    },
    filters: {

    }

}

</script>

<style lang='less' scoped>
.backBtn {
    margin-left: 30px;
    display: flex;
    align-items: center;
}

.backIcon {
    font-size: 39.9975px;
    width: 36px;
    height: 36px;
}

.searchBox {
    position: fixed;
    top: 0;
    margin: 0 auto;
    padding-top: 63.9975px;
    width: 100%;
    height: 96px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: #fff;
    z-index: 99;
    box-shadow: 0 .9975px 6px #ccc;
}

.navTit {
    font-size: 36px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #333333;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.searchBox>div:last-child {
    margin-right: 30px;
}


.imgBox {
    position: relative;
    box-sizing: border-box;
    padding: 180px 24px 24px;
    display: flex;
    flex-direction: column;
    width: 100%;
    background-color: #ffb84f;
    background-image: url(../../assets/distribution/new/imgBg.png);
    background-size: 100% 100%;

}

.imgCon {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    margin-bottom: 24px !important;
    border-radius: 20.0025px;
    -webkit-border-radius: 20.0025px;
    -moz-border-radius: 20.0025px;
    -ms-border-radius: 20.0025px;
    -o-border-radius: 20.0025px;
}

.imgCon>img {
    width: 100%;
    border-radius: 15.9975px;
    -webkit-border-radius: 15.9975px;
    -moz-border-radius: 15.9975px;
    -ms-border-radius: 15.9975px;
    -o-border-radius: 15.9975px;
}

.saveDownBtn {
    width: 291.9975px !important;
    height: 69.9975px !important;
}

.downImg {
    position: absolute;
    bottom: 8.0025px;
    width: 96%;
    height: 192.9975px;
    z-index: 0;

}

.handleBtn {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 20.0025px;
    width: 100%;
    height: 69.9975px;
    z-index: 20;
}

.handleBtn>img:first-child {
    width: 375.9975px;
    height: 69.9975px;

}

.handleBtn>img:last-child {
    width: 306px;
    height: 69.9975px;

}



.imgBox>img:first-child {
    width: 100%;
    height: auto;
}

.savaBtn {
    margin-top: 24px;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.savaBtn>div {
    width: 300px;
    height: 69.9975px;
    border-radius: 39.9975px;
    color: #fff;
    font-size: 24px;
    text-align: center;
    line-height: 69.9975px;
    background-color: #E4652D;
    -webkit-border-radius: 39.9975px;
    -moz-border-radius: 39.9975px;
    -ms-border-radius: 39.9975px;
    -o-border-radius: 39.9975px;
}

.downIcon {
    position: relative;
    top: 3.9975px;
}

.emptyBox {
    padding-top: 99.9975px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font-size: 27.9975px;
    color: #333;
    height: 100%;
}

.emptyBox>img {
    margin-bottom: 39.9975px;
    width: 369.9975px;
    height: 576px;
}


.jiangImg {
    position: fixed;
    top: 1200px;
    right: 20.0025px;
    width: 111.9975px;
    z-index: 40;
}


.jiangBtn {
    position: fixed;
    bottom: 50.0025px;
    left: 240px;
    width: 291.9975px;
    height: 69.9975px;
    z-index: 30;
}

.jiangImg {
    animation: round_animate 1.5s linear infinite;
    -webkit-animation: round_animate 1.5s linear infinite;
}

@keyframes round_animate {
    1% {
        transform: rotate(-10deg);
        -webkit-transform: rotate(-10deg);
        -moz-transform: rotate(-10deg);
        -ms-transform: rotate(-10deg);
        -o-transform: rotate(-10deg);
    }

    8% {
        transform: rotate(0deg);
        -webkit-transform: rotate(0deg);
        -moz-transform: rotate(0deg);
        -ms-transform: rotate(0deg);
        -o-transform: rotate(0deg);
    }

    16% {
        transform: rotate(10deg);
        -webkit-transform: rotate(10deg);
        -moz-transform: rotate(10deg);
        -ms-transform: rotate(10deg);
        -o-transform: rotate(10deg);
    }

    25% {
        transform: rotate(0deg);
        -webkit-transform: rotate(0deg);
        -moz-transform: rotate(0deg);
        -ms-transform: rotate(0deg);
        -o-transform: rotate(0deg);
    }


    100% {
        transform: rotate(0deg);
        -webkit-transform: rotate(0deg);
        -moz-transform: rotate(0deg);
        -ms-transform: rotate(0deg);
        -o-transform: rotate(0deg);
    }
}

.ruleBox {
    position: relative;
    box-sizing: border-box;
    width: 100%;
    min-height: 476.0025px;
    /* padding: 24px 60px; */
    /* background-color: #E25211; */
    background: linear-gradient(180deg, #ED7F10, #E4652D);

}

.topImg {
    position: absolute;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 236.0025px;
    z-index: 10;
}

.leftImg {
    position: absolute;
    right: 0px;
    top: 0px;
    width: 186px;
    height: 476.0025px;
    z-index: 9;
}

.bottomImg {
    position: absolute;
    bottom: 0px;
    left: 0px;
    width: 750px;
    height: 266.0025px;
    z-index: 11;
}

.ruleTitle {
    position: absolute;
    top: 48px;
    left: 309.9975px;
    width: 150px;
    height: 36px;
}

.rulesCon {
    display: inline-block;
    box-sizing: border-box;
    padding: 116.0025px 50.0025px 129.9975px 50.0025px;
    width: 100%;
    z-index: 10;
    font-size: 24px;
    font-family: Source Han Sans CN;
    font-weight: 500;
    color: #624C22;
    line-height: 32.0025px;
    overflow-y: scroll;
    background: linear-gradient(180deg, #F7E8BE 0%, #FFFFFF 98.779296875%);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.rulesCon::-webkit-scrollbar {
    display: none;
}



.showdiaBox {
    box-sizing: border-box;
    padding-top: 80.0025px;
    display: flex;
    align-items: center;
    justify-content: center;
    top: 0rem;
    left: 0rem;
    width: 100%;
    height: 100vh;
    background-color: rgba(0, 0, 0, 0.8);
    z-index: 30000;
    overflow: hidden;

}

.dialog {
    width: 576px;
    background: #F8E3E6;
    position: relative;
    border-radius: .2667rem;
    padding-bottom: .7467rem;
    -webkit-border-radius: .2667rem;
    -moz-border-radius: .2667rem;
    -ms-border-radius: .2667rem;
    -o-border-radius: .2667rem;
    z-index: 20;
}

.price-top {
    width: 639.9975px;
    position: absolute;
    left: 50%;
    margin-left: -320.0025px;
    top: -140.0025px;

}

.dia-title {
    font-size: 60px;
    font-weight: bold;
    color: #D9374E;
    text-align: center;
    padding-top: 80.0025px;
}

.dia-text {
    box-sizing: border-box;
    padding: 0 20.0025px;
    font-size: 27.9975px;
    padding-top: 15.9975px;
    text-align: center;
    color: #7C595E;
}

.dia-noprice {
    position: relative;
    margin-top: 44.0025px;
    width: 320.0025px;
    left: 50%;
    margin-left: -159.9975px;
    margin-bottom: 56.0025px;
}

.btnBox {
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: space-between;
    width: 576px;
}

.btnBox>div:first-child {
    margin-top: 39.9975px;
    width: 408px;
    height: 90px;
    line-height: 99.9975px;
    background: linear-gradient(0deg, #5C24B3, #8750DD);
    border-radius: 50.0025px;
    font-size: 27.9975px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #FFFFFF;
    text-align: center;
    -webkit-border-radius: .6667rem;
    -moz-border-radius: .6667rem;
    -ms-border-radius: .6667rem;
    -o-border-radius: .6667rem;
}



.upimgBox {
    position: relative;
    box-sizing: border-box;
    padding: 20.0025px;
    display: flex;
    margin: 39.9975px;
    width: 500.0025px;
    border: 2.0025px dashed #A77CEB;
}

.van-image {
    width: 135.9975px !important;
    height: 135.9975px !important;
}

.van-uploader {
    position: relative !important;
    width: 500.0025px;
    /* height: 249.9975px; */
}

.uplader {
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: center;
    width: 140.0025px;
    height: 140.0025px;
    font-size: 27.9975px;
    color: #A77CEB;
}

.uplader>.van-icon {
    font-size: 96px !important;
}

.firstUplader {
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: center;
    width: 459.9975px;
    font-size: 27.9975px;
    color: #A77CEB;

}

.firstUplader>.van-icon {
    font-size: 96px !important;
}

.waitBtn {
    margin-top: 20.0025px;
    font-size: 27.9975px;
    color: #999;
    border-bottom: .9975px solid #999;
}

.ruleImg {
    position: relative;
    top: 159.9975px;
    display: block;
    margin: 0rem auto;
    width: 189.9975px;
    height: 200.0025px;
}

.emptyBoxImg {
    position: relative;
    z-index: 30;
}

.dialog {
    box-sizing: border-box;
    /* padding: 20.0025px; */

    background: #f2f2f2;
    position: relative;
    border-radius: 9.9975px;
}

.diaImg {
    width: 100%;
    height: 50vh;

}

.diaImg>img {
    width: 200.0025px;
    height: 200.0025px;

}


.swiperBtn {
    position: relative;
    box-sizing: border-box;
    padding: 0 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    font-size: 27.9975px;
    color: #fff;
    z-index: 10;
}

.swiperBtn>div {
    display: flex;
    align-items: center;
}

.van-overlay {
    z-index: 1000;
}

.imgOverBox {
    box-sizing: border-box;
    padding-top: 80.0025px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    position: fixed;
    box-sizing: border-box;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100vh;
    background-color: rgba(0, 0, 0, 0.6);
    z-index: 30000;
    overflow-y: scroll;
}

.conBox {
    box-sizing: border-box;
    padding: 24px;
    width: 100%;
    height: 80vh;
    /* background: aqua; */
    overflow: scroll;
}

.conBox>img {
    width: 100% !important;

}

.imgBtnBox {
    box-sizing: border-box;
    padding: 0 30px;
    position: fixed;
    bottom: 0px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 750px;
    height: 116.0025px;
    background: #FFFFFF;
    box-shadow: 0px 2.0025px 8.0025px 0px rgba(51, 51, 51, 0.04);
    font-family: Source Han Sans CN;
    font-weight: 400;
    font-size: 27.9975px;
    color: #FFFFFF;
    line-height: 30px;
}

.imgBtnBox>div {
    display: flex;
    align-items: center;
    justify-content: center;
}

.imgBtnBox>div:first-child {
    width: 200.0025px;
    height: 75.9975px;
    box-shadow: 0px 2.0025px 8.0025px 0px rgba(51, 51, 51, 0.04);
    border-radius: 38.0025px;
    border: 2.0025px solid #999999;
    color: #333333;
}

.imgBtnBox>div:nth-child(2) {
    width: 434.0025px;
    height: 75.9975px;
    background: linear-gradient(95deg, #1CD0BA, #1CD0BA);
    box-shadow: 0px 2.0025px 8.0025px 0px rgba(51, 51, 51, 0.04);
    border-radius: 38.0025px;
    border: 2.0025px solid #1CD0BA;
}


.kefuImgBox {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    width: 600px;
    height: 600px;
}

.kefuImgBox>img {
    width: 399.9975px;
    height: 399.9975px;
    /* background-color: #1CD0BA; */
    border-radius: 15.9975px;
    -webkit-border-radius: 15.9975px;
    -moz-border-radius: 15.9975px;
    -ms-border-radius: 15.9975px;
    -o-border-radius: 15.9975px;
}

.kefuImgBox>div {
    margin-top: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 300px;
    height: 60px;
    background: linear-gradient(186deg, #F7E296 0%, #EB5521 100%);
    box-shadow: 0px 0px 3px 0px rgba(173, 49, 30, 0.42);
    border-radius: 12px;
    font-size: 27.9975px;
    color: #fff;
    -webkit-border-radius: 12px;
    -moz-border-radius: 12px;
    -ms-border-radius: 12px;
    -o-border-radius: 12px;
}

.kefuBtn {
    position: absolute;
    right: 0px;
    color: #fff;
    font-size: 27.9975px;
    font-weight: bold;
}


.header .van-icon {
    color: #333;
    font-size: 40px;
    font-weight: bold;
}

.header .van-nav-bar__text {
    color: #333;
}

.header {
    position: fixed;
    z-index: 10;
    width: 100%;
    height: 88px;
    background-color: #fff;
    // padding-top: 60px;
}

/deep/.van-nav-bar__text,
/deep/.van-nav-bar__left,
/deep/.van-nav-bar .van-icon {
    color: #333;
}
</style>